package dotdashdot.bradz.trendsettr;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

public class ClassTrendingContainer {

	private ArrayList<ClassTopic> topic_list;
	private ClassUberLogger logger;
	
	ClassTrendingContainer(){
		topic_list = new ArrayList<ClassTopic>();
		logger = new ClassUberLogger("ClassTrendingContainer");
		logger.info("Class Initialized");
	}
	ClassTrendingContainer(ArrayList<ClassTopic> topics){
		topic_list = new ArrayList<ClassTopic>(topics);
		logger = new ClassUberLogger("ClassTrendingContainer");
		logger.info("Class Initialized");
	}
	public ArrayList<ClassTopic> get_topic_list(){
		return topic_list;
	}
	//This is for the filter bar
	public String[] get_topic_names(){
		String[] topics = new String[topic_list.size()+1];
		topics[0] = "All";
		for(int i = 1; i < topic_list.size()+1;i++){
			topics[i] = topic_list.get(i-1).get_name();
		}
		return topics;
	}
	public void print_info(){
		logger.info("Printing Container Infooooo......");
		for(ClassTopic topic: topic_list){
			for(ClassTrend trend:topic.get_trend()){
				logger.info(topic.get_name() + ":" + trend.get_name());
			}
		}
	}
	public ClassTrendingContainer get_unseen_list(ArrayList<String> topics, boolean all_unlocked){
		ArrayList<ClassTopic> topic_list_holder = new ArrayList<ClassTopic>();
		logger.info("Collecting trends that haven't been voted on yet ");
		logger.info("Topic List Size: " + topic_list_holder.size());
		for(ClassTopic topic: topic_list){
			if(topics.contains(topic.get_name()) || all_unlocked){
				logger.info("Topic: " + topic.get_name());
				ArrayList<ClassTrend> trends = new ArrayList<ClassTrend>(topic.get_unused_trends());
				logger.info("Trend Size: " + trends.size());
				if(trends.size()>0){
					topic_list_holder.add(new ClassTopic(topic.get_name(),trends));
					for(ClassTrend trend:trends){
						logger.info("Topic: " + topic.get_name() + " Trend: " + trend.get_name());
					}
				}
			}
		}
		logger.info("Returning trends");
		return new ClassTrendingContainer(topic_list_holder);
	}
	
	public void set_trend_vote(int topic_index,int trend_index, int vote){
		if(topic_index != -1 && trend_index != -1){ 
			String trend_name = topic_list.get(topic_index).get_trend().get(trend_index).get_name();
			topic_list.get(topic_index).get_trend().get(trend_index).get_info().vote = vote;
			logger.info("Attempting to save vote...");
			//Log.d("Received Topic Vote","Trend Vote Index: " + index);
			logger.info("Trend Vote Name: " + trend_name);
			logger.info("Trend Vote: " + vote);
			topic_list.get(topic_index).get_trend().remove(trend_index);
			if(topic_list.get(topic_index).get_trend().size()<1){
				topic_list.remove(topic_index);
			}
		}
	}
	public void set_trend_vote(String topic_string,String trend_string, int vote){
		int topic_index = -1;
		int trend_index = -1;
		for(ClassTopic topic:topic_list){
			logger.info("Comparing Topic: " + topic_string + " : " + topic.get_name());
			if(topic.get_name().equals(topic_string)){
				topic_index = topic_list.indexOf(topic);
				for(ClassTrend trend:topic.get_trend()){
					logger.info("Comparing Trend: " + trend_string + " : " + trend.get_name());
					if(trend.get_name().equals(trend_string)){
						trend.get_info().vote = vote;
						logger.info("Received Trend Vote. Voted: " + vote);
						break;
					}
				}
			}
		}
		Log.d("Received Topic Vote","Finished");
	}
	public ClassTopic get_topic(String name){
		logger.info("Searching for topic");
		for(ClassTopic topic: topic_list){
			if(topic.get_name().equals(name)){
				logger.info("Found: "+name);
				return topic;
			}
		}
		return null;
	}
	public void add_topic(String name){
		//If there is no error, we need to add the topic to the container
		boolean duplicate_topic = false;
		ClassTopic new_topic = new ClassTopic(name, new ArrayList<ClassTrend>());
		for(ClassTopic topic:topic_list){
			if(topic.get_name().equals(name)){
				duplicate_topic = true;
			}
		}
		if(!duplicate_topic){
			topic_list.add(new_topic);
		}
	}
	
}
